//
//  DownloadTableViewCell.m
//  MXClassroom
//
//  Created by 你个LB on 16/9/11.
//  Copyright © 2016年 你个LB. All rights reserved.
//

#import "DownloadTableViewCell.h"

@implementation DownloadTableViewCell

/** 初始化方法 */
+ (instancetype)cellWithTableView:(UITableView *)tableView{
    static NSString * reuseIdentifier = @"identifier";
    DownloadTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (cell == nil) {
        // 创建cell
        cell = [[DownloadTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
    }
    return cell;
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // 图标
        _headImgView = [[UIImageView alloc] initWithFrame:CGRectMake(8, 13, 60, 60)];
        //        _headImgView.backgroundColor = [UIColor redColor];
        [self.contentView addSubview:_headImgView];
        
        // 标题
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(_headImgView.right + 16, 13, 150, 13)];
        _titleLabel.textColor = kColor_text_color;
        _titleLabel.font = [UIFont systemFontOfSize:13 weight:2];
        //        _titleLabel.backgroundColor = [UIColor redColor];
        [self.contentView addSubview:_titleLabel];
        
        // 讲师
        _teacherLabel = [[UILabel alloc] initWithFrame:CGRectMake(_headImgView.right + 16, _titleLabel.bottom + 13, 100, 11)];
//        _teacherLabel.bottom = _headImgView.bottom;
        _teacherLabel.textColor = kColor_text_color;
        _teacherLabel.font = [UIFont systemFontOfSize:11 weight:2];
        //        _teacherLabel.backgroundColor = [UIColor redColor];
        [self.contentView addSubview:_teacherLabel];
        
        // 讲解时间
        _teachTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(_headImgView.right + 16, 0, 60, 11)];
        _teachTimeLabel.bottom = _headImgView.bottom;
        _teachTimeLabel.textColor = kColor_text_color;
        _teachTimeLabel.font = [UIFont systemFontOfSize:11];
        //        _teachTimeLabel.backgroundColor = [UIColor redColor];
        [self.contentView addSubview:_teachTimeLabel];
        
        // 文件大小
        _fileSizeLabel = [[UILabel alloc] initWithFrame:CGRectMake(kScreen_width - 50 - 8, 14, 50, 11)];
        _fileSizeLabel.textColor = kColor_text_color;
        _fileSizeLabel.font = [UIFont systemFontOfSize:11];
        _fileSizeLabel.textAlignment = NSTextAlignmentRight;
//        _fileSizeLabel.backgroundColor = [UIColor redColor];
        [self.contentView addSubview:_fileSizeLabel];
        
        // 删除按钮
        UIButton * deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        deleteBtn.frame = CGRectMake(kScreen_width - 30 - 8, _fileSizeLabel.bottom + 13, 30, 30);
        [deleteBtn setBackgroundImage:[UIImage imageNamed:@"下载中_删除87px.png"] forState:UIControlStateNormal];
//        deleteBtn.backgroundColor = [UIColor redColor];
        [self.contentView addSubview:deleteBtn];
        
        // 创建等待下载试图
        [self waitDownloadView];

        // 创建正在下载试图
        [self downloadingView];
    }
    return self;
}

// 等待下载试图
- (void)waitDownloadView{
    // 等待下载
    _waitDownloadView = [[UIView alloc] initWithFrame:CGRectMake(6, 86, kScreen_width, 30)];
    [self.contentView addSubview:_waitDownloadView];
    
    // 横线条
    UIView * lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 86, kScreen_width, 0.5)];
    [_waitDownloadView addSubview:lineView];
    

    // 等待下载图标
    UIImageView * iconWaitDownload = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 20, 20)];
    iconWaitDownload.image = [UIImage imageNamed:@"下载中_等待下载55px.png"];
    [_waitDownloadView addSubview:iconWaitDownload];
    
    
    // 提示文本
    UILabel * messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(iconWaitDownload.right + 2, 0, 50, 30)];
    messageLabel.text = @"等待下载";
    messageLabel.textColor = kColor_Memory_text_color;
    messageLabel.font = [UIFont systemFontOfSize:11];
    [_waitDownloadView addSubview:messageLabel];
}

// 正在下载试图
- (void)downloadingView{
    // 等待下载
    _downloadingView = [[UIView alloc] initWithFrame:CGRectMake(0, 86, kScreen_width, 30)];
//    _downloadingView.backgroundColor = [UIColor redColor];
    [self.contentView addSubview:_downloadingView];
    
    // 横线条
    UIView * lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreen_width, 0.5)];
    [_downloadingView addSubview:lineView];
    
    
    // 正在图标
    UIImageView * iconWaitDownload = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 20, 20)];
    iconWaitDownload.image = [UIImage imageNamed:@"下载中_正在下载55px.png"];
    [_downloadingView addSubview:iconWaitDownload];
    
    
    // 提示文本
    UILabel * messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(iconWaitDownload.right + 2, 0, 50, 30)];
    messageLabel.text = @"正在下载";
    messageLabel.textColor = kColor_Memory_text_color;
    messageLabel.font = [UIFont systemFontOfSize:11];
    [_downloadingView addSubview:messageLabel];
    
    // 下载文件大小
    _downloadFileSizeLabel = [[UILabel alloc] initWithFrame:CGRectMake(kScreen_width - 70 - 5, 0, 70, 30)];
    _downloadFileSizeLabel.textColor = kColor_Memory_text_color;
    _downloadFileSizeLabel.font = [UIFont systemFontOfSize:11];
    [_downloadingView addSubview:_downloadFileSizeLabel];
    
    
    // 进度条
    _progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(messageLabel.right + 2, 13, kScreen_width - 20 - 50 - 70 - 4 * 5 - 2, 20)];
    _progressView.transform = CGAffineTransformScale(_progressView.transform, 1.0, 2.0);
    [_downloadingView addSubview:_progressView];
}

- (void)setDownload:(DownloadModel *)download{
    if (_download != download) {
        _download = download;
        
        // 为控件设置数据
        _headImgView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",download.headImg]];
        
        _titleLabel.text = download.title;
        
        _teachTimeLabel.text = download.teachTime;
        
        _teacherLabel.text = [NSString stringWithFormat:@"by %@",download.teacher];
        
        _fileSizeLabel.text = [NSString stringWithFormat:@"%0.1fM",download.fileSize];
        
        if (download.downloadState == WaitDownload) {
            // 等待下载状态
            _downloadingView.hidden = YES;
            _waitDownloadView.hidden = NO;
        } else if (download.downloadState == Downloading){
            // 下载中状态
            _downloadingView.hidden = NO;
            _waitDownloadView.hidden = YES;
            _progressView.progress = download.downloadProgress;
            // 当前已经下载文件体积
            float downloadedFileSize = download.fileSize  *  download.downloadProgress;
            
            _downloadFileSizeLabel.text = [NSString stringWithFormat:@"%0.1fM / %0.1f",downloadedFileSize, download.fileSize];
        } else{
            // 下载完成状态
            _downloadingView.hidden = YES;
            _waitDownloadView.hidden = YES;
        }
    }
}

@end
